package com.lyz.dataStructure.LeetCode;

import java.util.Scanner;

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2019/4/12 18:55
 *@Description:
 **/

/*
第一行包含一个正整数n，即树的结点数量（1<=n<=100000）。

接下来有n-1行，每行有两个正整数x，y，表示在x和y结点之间存在一条边。(1<=x，y<=n)

6
2 1
3 2
4 3
5 2
6 1

 */


public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int m = scanner.nextInt();
            TreeNode rootNode = new TreeNode(scanner.nextInt());
            cTree(rootNode, rootNode.val, scanner.nextInt());
            for(int i=1;i<m-1;i++){
                cTree(rootNode, scanner.nextInt(), scanner.nextInt());
            }

            int time = getShortTime(rootNode);
            System.out.println(time);
        }
        scanner.close();
    }

    /**
     * 求所需时间
     * @param rootNode
     * @return
     */
    private static int getShortTime(TreeNode rootNode) {
        if(rootNode==null){
            return 0;
        }
        int nLeft = getShortTime(rootNode.left);
        int nRight = getShortTime(rootNode.right);
        if(nLeft>nRight){
            return nLeft+2;
        }else{
            return nRight+2;
        }
    }

    /**
     * 建树
     * @param root
     * @param father
     * @param cVal
     */
    public static void cTree(TreeNode root,int father,int cVal){
        if(root==null){
            return;
        }
        if(root!=null && root.val==father){
            if(root.left==null){
                root.left = new TreeNode(cVal);
            }else{
                root.right = new TreeNode(cVal);
            }
            return;
        }

        cTree(root.left, father, cVal);
        cTree(root.right, father, cVal);
    }
}

class TreeNode{
    TreeNode left;
    TreeNode right;
    int val;

    public TreeNode(int val) {
        this.val = val;
    }

}